---
title: "Bash String 操作"
date: 2020-2-4
categories:
- linux
- shell
tags:
---

<div id="content">
<div id="table-of-contents">
<h2>Table of Contents</h2>
<div id="text-table-of-contents">
<ul>
<li><a href="#orge68572d">基本操作</a>
<ul>
<li><a href="#orgabb4294">参数长度: {% raw %} ${#parameter} {% endraw %}</a></li>
</ul>
</li>
<li><a href="#org9f363fb">Substring</a>
<ul>
<li><a href="#org26c47a1">{% raw %} ${parameter:offset:length} {% endraw %}</a>
<ul>
<li><a href="#org685f9f6">offset</a></li>
<li><a href="#orge40ef99">length</a></li>
<li><a href="#org1d459bb">Example</a></li>
</ul>
</li>
<li><a href="#org92ed905">{% raw %} ${parameter#word} {% endraw %}</a></li>
<li><a href="#org5329019">{% raw %} o${parameter%word} {% endraw %}</a></li>
</ul>
</li>
<li><a href="#org5c26a48">Replace</a>
<ul>
<li><a href="#org158b350">{% raw %} ${parameter/pattern/string} {% endraw %}</a></li>
</ul>
</li>
</ul>
</div>
</div>
<div class="outline-2" id="outline-container-orge68572d">
<h2 id="orge68572d">基本操作</h2>
<div class="outline-text-2" id="text-orge68572d">
</div>
<div class="outline-3" id="outline-container-orgabb4294">
<h3 id="orgabb4294">参数长度: {% raw %} ${#parameter} {% endraw %}</h3>
<div class="outline-text-3" id="text-orgabb4294">
<p>
能够计算字符串参数的长度，也能计算数组的长度。parameter也能传*, @。
</p>
<div class="org-src-container">
<pre class="src src-sh"><span style="font-weight: bold; font-style: italic;">str</span>=<span style="font-style: italic;">"asdf"</span>
<span style="font-weight: bold;">echo</span>{% raw %}  ${# {% endraw %}<span style="font-weight: bold; font-style: italic;">str</span>}
4
</pre>
</div>
</div>
</div>
</div>
<div class="outline-2" id="outline-container-org9f363fb">
<h2 id="org9f363fb">Substring</h2>
<div class="outline-text-2" id="text-org9f363fb">
<p>
See bash man document at line 777 <b>Parameter Expansion</b>
</p>
</div>
<div class="outline-3" id="outline-container-org26c47a1">
<h3 id="org26c47a1">{% raw %} ${parameter:offset:length} {% endraw %}</h3>
<div class="outline-text-3" id="text-org26c47a1">
</div>
<div class="outline-4" id="outline-container-org685f9f6">
<h4 id="org685f9f6">offset</h4>
<div class="outline-text-4" id="text-org685f9f6">
<p>
offset 从0开始。
如果offset小于0，则从字符串尾向前计算偏移量，小于的表示需要在负数前加上0,如下例。
</p>
</div>
</div>
<div class="outline-4" id="outline-container-orge40ef99">
<h4 id="orge40ef99">length</h4>
<div class="outline-text-4" id="text-orge40ef99">
<p>
省略表示从offset到结尾。
</p>
<p>
负数则被处理为从结尾开始的偏移量，而不再是长度。
</p>
</div>
</div>
<div class="outline-4" id="outline-container-org1d459bb">
<h4 id="org1d459bb">Example</h4>
<div class="outline-text-4" id="text-org1d459bb">
<div class="org-src-container">
<pre class="src src-bash"><span style="font-weight: bold; font-style: italic;">str</span>=<span style="font-style: italic;">'hello-world'</span>

<span style="font-weight: bold;">echo</span> ${<span style="font-weight: bold; font-style: italic;">str</span>:1:5}
<span style="font-weight: bold; font-style: italic;"># </span><span style="font-weight: bold; font-style: italic;">ello-</span>

<span style="font-weight: bold;">echo</span> ${<span style="font-weight: bold; font-style: italic;">str</span>:0-5:2}
<span style="font-weight: bold; font-style: italic;"># </span><span style="font-weight: bold; font-style: italic;">wo</span>

<span style="font-weight: bold;">echo</span> ${<span style="font-weight: bold; font-style: italic;">str</span>:1:-2}
<span style="font-weight: bold; font-style: italic;"># </span><span style="font-weight: bold; font-style: italic;">ello-wor</span>
</pre>
</div>
</div>
</div>
</div>
<div class="outline-3" id="outline-container-org92ed905">
<h3 id="org92ed905">{% raw %} ${parameter#word} {% endraw %}</h3>
<div class="outline-text-3" id="text-org92ed905">
<p>
从左边开始匹配，移除匹配的部分，返回剩下的字符
两个 # 表示最长匹配
</p>
<div class="org-src-container">
<pre class="src src-sh"><span style="font-weight: bold; font-style: italic;">str</span>=<span style="font-style: italic;">'hw-1.2.0.tar.gz'</span>

<span style="font-weight: bold;">echo</span> ${<span style="font-weight: bold; font-style: italic;">str</span>#*.tar}
<span style="font-weight: bold; font-style: italic;"># </span><span style="font-weight: bold; font-style: italic;">.gz</span>

<span style="font-weight: bold;">echo</span> ${<span style="font-weight: bold; font-style: italic;">str</span>#*.}
<span style="font-weight: bold; font-style: italic;"># </span><span style="font-weight: bold; font-style: italic;">2.0.tar.gz</span>

<span style="font-weight: bold;">echo</span> ${<span style="font-weight: bold; font-style: italic;">str</span>##*.}
<span style="font-weight: bold; font-style: italic;"># </span><span style="font-weight: bold; font-style: italic;">gz</span>

</pre>
</div>
</div>
</div>
<div class="outline-3" id="outline-container-org5329019">
<h3 id="org5329019">{% raw %} o${parameter%word} {% endraw %}</h3>
<div class="outline-text-3" id="text-org5329019">
<p>
与 {% raw %} ${parameter#word} {% endraw %} 相反，从右边开始
</p>
<div class="org-src-container">
<pre class="src src-bash"><span style="font-weight: bold; font-style: italic;">str</span>=<span style="font-style: italic;">'hw-1.2.0.tar.gz'</span>

<span style="font-weight: bold;">echo</span> ${<span style="font-weight: bold; font-style: italic;">str</span>%.*}
<span style="font-weight: bold; font-style: italic;"># </span><span style="font-weight: bold; font-style: italic;">hw-1.2.0.tar</span>

<span style="font-weight: bold;">echo</span> ${<span style="font-weight: bold; font-style: italic;">str</span>%%.*}
<span style="font-weight: bold; font-style: italic;"># </span><span style="font-weight: bold; font-style: italic;">hw-1</span>

</pre>
</div>
</div>
</div>
</div>
<div class="outline-2" id="outline-container-org5c26a48">
<h2 id="org5c26a48">Replace</h2>
<div class="outline-text-2" id="text-org5c26a48">
</div>
<div class="outline-3" id="outline-container-org158b350">
<h3 id="org158b350">{% raw %} ${parameter/pattern/string} {% endraw %}</h3>
</div>
</div>
</div>
<div class="status" id="postamble">
<p class="date">Date: 2020-2-4</p>
<p class="author">Author: gdme1320</p>
<p class="validation"><a href="http://validator.w3.org/check?uri=referer">Validate</a></p>
</div>
